<template>
  <div class="account-approval-page">
    <!-- Header -->
    <header class="page-header">
      <div class="container">
        <div class="header-content">
          <div class="header-left">
            <button @click="navigateBack" class="btn-back">
              ← 返回
            </button>
            <div class="header-info">
              <h1>账户申请审核</h1>
              <p>审核用户的账户申请并批准或拒绝</p>
            </div>
          </div>
          <div class="header-actions">
            <button 
              @click="loadApplications" 
              class="btn-load-applications"
              :disabled="isLoading"
            >
              <span v-if="isLoading" class="loading-spinner"></span>
              {{ isLoading ? '加载中...' : '加载申请' }}
            </button>
          </div>
        </div>
      </div>
    </header>

    <!-- Main Content -->
    <main class="page-main">
      <div class="container">
        <!-- Stats Section -->
        <div class="stats-section">
          <div class="stat-card">
            <div class="stat-value">{{ applications.length }}</div>
            <div class="stat-label">待审核申请</div>
          </div>
          <div class="stat-card">
            <div class="stat-value">{{ approvedCount }}</div>
            <div class="stat-label">已批准</div>
          </div>
          <div class="stat-card">
            <div class="stat-value">{{ rejectedCount }}</div>
            <div class="stat-label">已拒绝</div>
          </div>
        </div>

        <!-- Error Message -->
        <div class="error-message" v-if="error">
          <p>{{ error }}</p>
          <button @click="loadApplications" class="btn-retry">重试</button>
        </div>

        <!-- AG Grid Section -->
        <div class="grid-section" v-if="applications.length > 0">
          <div class="grid-header">
            <div class="grid-title">
              <h2>申请列表</h2>
              <span class="grid-count">共 {{ applications.length }} 条记录</span>
            </div>
            <div class="grid-actions">
              <div class="bulk-actions">
                <button 
                  @click="selectAllRows" 
                  class="btn-select-all"
                  :class="{ 'active': isAllSelected }"
                >
                  {{ isAllSelected ? '取消全选' : '全选' }}
                </button>
                <button 
                  @click="clearSelection" 
                  class="btn-clear"
                  :disabled="!selectedApplications.length"
                >
                  清空选择
                </button>
              </div>
              <div class="action-buttons">
                <button 
                  @click="approveSelectedApplications" 
                  class="btn-approve"
                  :disabled="!selectedApplications.length || isSubmitting"
                >
                  <span v-if="isApproving" class="loading-spinner"></span>
                  {{ isApproving ? '批准中...' : `批准所选 (${selectedApplications.length})` }}
                </button>
                <button 
                  @click="rejectSelectedApplications" 
                  class="btn-reject"
                  :disabled="!selectedApplications.length || isSubmitting"
                >
                  <span v-if="isRejecting" class="loading-spinner"></span>
                  {{ isRejecting ? '拒绝中...' : `拒绝所选 (${selectedApplications.length})` }}
                </button>
              </div>
            </div>
          </div>

          <!-- AG Grid -->
          <div class="ag-grid-container">
            <ag-grid-vue
              ref="agGrid"
              class="ag-theme-balham"
              style="height: 500px; width: 100%;"
              :columnDefs="columnDefs"
              :rowData="applications"
              :defaultColDef="defaultColDef"
              rowSelection="multiple"
              :suppressRowClickSelection="true"
              :animateRows="true"
              :pagination="true"
              :paginationPageSize="20"
              :getRowId="getRowId"
              @grid-ready="onGridReady"
              @selection-changed="onSelectionChanged"
              @row-clicked="onRowClicked"
            />
          </div>
        </div>

        <!-- Empty State -->
        <div class="empty-state" v-else-if="!isLoading && hasLoadedOnce">
          <div class="empty-icon">📝</div>
          <h3>暂无待审核申请</h3>
          <p>当前没有需要审核的账户申请</p>
          <button @click="loadApplications" class="btn-reload">
            重新加载
          </button>
        </div>

        <!-- Loading State -->
        <div class="loading-state" v-else-if="isLoading">
          <div class="loading-spinner large"></div>
          <p>加载申请中...</p>
        </div>
      </div>
    </main>

    <!-- Approval Confirmation Modal -->
    <div class="modal" v-if="showApproveModal">
      <div class="modal-content">
        <div class="modal-header">
          <h2>确认批准</h2>
          <button class="modal-close" @click="showApproveModal = false">&times;</button>
        </div>
        <div class="modal-body">
          <p>您确定要批准以下 <strong>{{ selectedApplications.length }}</strong> 个账户申请吗？</p>
          
          <div class="selected-applications-preview">
            <div class="preview-header">选中的申请：</div>
            <div class="preview-list">
              <div 
                v-for="application in selectedApplications.slice(0, 5)" 
                :key="application.applicationId"
                class="preview-item"
              >
                {{ application.accountTypeName }} (申请人: {{ application.username || application.userId }})
              </div>
              <div v-if="selectedApplications.length > 5" class="preview-more">
                还有 {{ selectedApplications.length - 5 }} 个申请...
              </div>
            </div>
          </div>
          
          <div class="form-actions">
            <button type="button" class="btn-cancel" @click="showApproveModal = false">
              取消
            </button>
            <button type="button" class="btn-confirm approve" @click="confirmApprove">
              确认批准
            </button>
          </div>
        </div>
      </div>
    </div>

    <!-- Rejection Modal -->
    <div class="modal" v-if="showRejectModal">
      <div class="modal-content">
        <div class="modal-header">
          <h2>确认拒绝</h2>
          <button class="modal-close" @click="showRejectModal = false">&times;</button>
        </div>
        <div class="modal-body">
          <p>您确定要拒绝以下 <strong>{{ selectedApplications.length }}</strong> 个账户申请吗？</p>
          
          <div class="selected-applications-preview">
            <div class="preview-header">选中的申请：</div>
            <div class="preview-list">
              <div 
                v-for="application in selectedApplications.slice(0, 5)" 
                :key="application.applicationId"
                class="preview-item"
              >
                {{ application.accountTypeName }} (申请人: {{ application.username || application.userId }})
              </div>
              <div v-if="selectedApplications.length > 5" class="preview-more">
                还有 {{ selectedApplications.length - 5 }} 个申请...
              </div>
            </div>
          </div>
          
          <div class="rejection-reason">
            <label for="rejectionReason">拒绝原因：</label>
            <textarea 
              id="rejectionReason" 
              v-model="rejectionReason" 
              placeholder="请输入拒绝原因..."
              rows="3"
            ></textarea>
          </div>
          
          <div class="form-actions">
            <button type="button" class="btn-cancel" @click="showRejectModal = false">
              取消
            </button>
            <button 
              type="button" 
              class="btn-confirm reject" 
              @click="confirmReject"
              :disabled="!rejectionReason.trim()"
            >
              确认拒绝
            </button>
          </div>
        </div>
      </div>
    </div>

    <!-- Success Modal -->
    <div class="modal" v-if="showSuccessModal">
      <div class="modal-content success-modal">
        <div class="modal-header">
          <h2>{{ actionSuccessTitle }}</h2>
          <button class="modal-close" @click="showSuccessModal = false">&times;</button>
        </div>
        <div class="modal-body">
          <div class="success-icon">✓</div>
          <p>{{ actionSuccessMessage }}</p>
          
          <div class="form-actions">
            <button type="button" class="btn-confirm" @click="closeSuccessModal">
              确定
            </button>
          </div>
        </div>
      </div>
    </div>

    <!-- Modal Overlay -->
    <div 
      class="modal-overlay" 
      v-if="showApproveModal || showRejectModal || showSuccessModal" 
      @click="closeAllModals"
    ></div>
  </div>
</template>

<script setup>
import { ModuleRegistry, AllCommunityModule } from 'ag-grid-community';
ModuleRegistry.registerModules([ AllCommunityModule ]);
import 'ag-grid-community/styles/ag-theme-balham.css'
import { ref, computed, onMounted, watch } from 'vue';
import { AgGridVue } from 'ag-grid-vue3';
import 'ag-grid-community/styles/ag-grid.css';
import 'ag-grid-community/styles/ag-theme-alpine.css';
import accountService from '../../services/accountService';
import authService from '../../services/authService';
import { useRouter } from 'vue-router';

const router = useRouter();

// 响应式数据
const applications = ref([]);
const selectedApplications = ref([]);
const isLoading = ref(false);
const isApproving = ref(false);
const isRejecting = ref(false);
const hasLoadedOnce = ref(false);
const showApproveModal = ref(false);
const showRejectModal = ref(false);
const showSuccessModal = ref(false);
const actionSuccessTitle = ref('');
const actionSuccessMessage = ref('');
const rejectionReason = ref('');
const approvedCount = ref(0);
const rejectedCount = ref(0);
const error = ref(null);
const agGrid = ref(null);
const gridApi = ref(null);

// 计算属性
const isAllSelected = computed(() => {
  return applications.value.length > 0 && selectedApplications.value.length === applications.value.length;
});

const isSubmitting = computed(() => {
  return isApproving.value || isRejecting.value;
});

// AG Grid 配置
const columnDefs = ref([
  {
    headerName: '',
    checkboxSelection: true,
    headerCheckboxSelection: true,
    width: 50,
    pinned: 'left',
    lockPosition: true,
    suppressMenu: true,
    suppressSorting: true,
    suppressFilter: true
  },
  {
    headerName: '申请ID',
    field: 'applicationId',
    width: 100,
    pinned: 'left',
    cellClass: 'application-id-cell',
    suppressSizeToFit: true
  },
  {
    headerName: '用户ID',
    field: 'userId',
    width: 100,
    cellClass: 'user-id-cell',
    filter: 'agTextColumnFilter'
  },
  {
    headerName: '用户名',
    field: 'username',
    width: 150,
    cellClass: 'username-cell',
    filter: 'agTextColumnFilter'
  },
  {
    headerName: '账户类型',
    field: 'accountTypeName',
    width: 150,
    filter: 'agTextColumnFilter'
  },
  {
    headerName: '初始存款',
    field: 'initialDeposit',
    width: 120,
    filter: 'agNumberColumnFilter',
    valueFormatter: params => formatCurrency(params.value)
  },
  {
    headerName: '用途',
    field: 'purpose',
    width: 200,
    filter: 'agTextColumnFilter'
  },
  {
    headerName: '资金来源',
    field: 'sourceOfFunds',
    width: 150,
    filter: 'agTextColumnFilter'
  },
  {
    headerName: '申请日期',
    field: 'applicationDate',
    width: 160,
    filter: 'agDateColumnFilter',
    valueFormatter: params => formatDateTime(params.value)
  },
  {
    headerName: '状态',
    field: 'status',
    width: 120,
    filter: 'agTextColumnFilter',
    cellRenderer: params => {
      const status = params.value;
      let statusClass = '';
      let statusText = '';
      
      switch(status) {
        case 'PENDING':
          statusClass = 'status-pending';
          statusText = '待审核';
          break;
        case 'APPROVED':
          statusClass = 'status-approved';
          statusText = '已批准';
          break;
        case 'REJECTED':
          statusClass = 'status-rejected';
          statusText = '已拒绝';
          break;
        default:
          statusClass = 'status-unknown';
          statusText = status || '未知';
      }
      
      return `<div class="status-badge ${statusClass}">${statusText}</div>`;
    }
  }
]);

const defaultColDef = ref({
  sortable: true,
  filter: true,
  resizable: true,
  minWidth: 100,
  flex: 1
});

// Watch for changes in applications data
watch(() => applications.value, (newApplications) => {
  console.log('Applications data changed:', newApplications);
  
  // If grid is ready, update the row data
  if (gridApi.value) {
    console.log('Updating grid data');
    gridApi.value.setRowData?.(newApplications) || console.log('setRowData not available, will update on next render');
  }
  
  // Update counts
  updateCounts();
}, { deep: true });

// Function to get unique row ID
function getRowId(params) {
  return params.data.applicationId;
}

// AG Grid 方法
function onGridReady(params) {
  console.log('AG Grid ready');
  gridApi.value = params.api;
  
  // Log grid API to verify it's available
  console.log('Grid API initialized:', !!gridApi.value);
  
  // Force refresh to ensure proper rendering
  setTimeout(() => {
    if (gridApi.value) {
      console.log('Refreshing grid cells');
      if (typeof gridApi.value.refreshCells === 'function') {
        gridApi.value.refreshCells();
      }
    }
  }, 100);
}

function onSelectionChanged(event) {
  console.log('Selection changed event triggered');
  if (!event.api) return;
  
  // Get selected rows directly from the grid API
  const selectedRows = event.api.getSelectedRows();
  console.log('Selected rows from grid:', selectedRows);
  
  // Create a completely new array to ensure reactivity
  selectedApplications.value = [...selectedRows];
  
  console.log('Updated selectedApplications:', selectedApplications.value);
}

function onRowClicked(event) {
  if (!event.data) return;
  console.log('Row clicked:', event.data);
}

function selectAllRows() {
  if (!gridApi.value) return;
  
  if (isAllSelected.value) {
    console.log('Deselecting all rows');
    gridApi.value.deselectAll();
  } else {
    console.log('Selecting all rows');
    gridApi.value.selectAll();
  }
}

function clearSelection() {
  if (!gridApi.value) return;
  console.log('Clearing selection');
  gridApi.value.deselectAll();
}

// 其他方法
async function loadApplications() {
  isLoading.value = true;
  error.value = null;
  
  try {
    console.log('Fetching account applications...');
    const response = await accountService.getPendingAccountApplications();
    console.log('Received response:', response);
    
    // Ensure the response is an array
    if (!Array.isArray(response.data)) {
      console.error('Expected array response but got:', typeof response.data);
      throw new Error('Invalid response format');
    }

    // Log the first item to check structure
    if (response.data.length > 0) {
      console.log('Sample application data:', response.data[0]);
    }
    
    applications.value = response.data;
    hasLoadedOnce.value = true;
    
    // 清空之前的选择
    selectedApplications.value = [];
    
    // Update grid data if grid is ready
    if (gridApi.value) {
      gridApi.value.setRowData(response.data);
    }
    
    // Update counts
    updateCounts();
    
    console.log('Updated applications array:', applications.value);
  } catch (err) {
    error.value = err.message || 'Failed to load account applications';
    console.error('Error loading account applications:', err);
  } finally {
    isLoading.value = false;
  }
}

function updateCounts() {
  approvedCount.value = applications.value.filter(app => app.status === 'APPROVED').length;
  rejectedCount.value = applications.value.filter(app => app.status === 'REJECTED').length;
}

function approveSelectedApplications() {
  if (selectedApplications.value.length === 0) {
    return;
  }
  
  showApproveModal.value = true;
}

function rejectSelectedApplications() {
  if (selectedApplications.value.length === 0) {
    return;
  }
  
  showRejectModal.value = true;
  rejectionReason.value = ''; // Clear previous rejection reason
}

async function confirmApprove() {
  isApproving.value = true;
  showApproveModal.value = false;
  
  try {
    // Extract application IDs
    const applicationIds = selectedApplications.value.map(app => app.applicationId);
    console.log('Approving applications:', applicationIds);
    
    // Create the request object
    const requestData = {
      applicationIds: applicationIds,
      status: 'APPROVED',
      // notes: '管理员批准',
      approvedBy: 'admin',
      rejectionReason: ''

    };
    
    // Make API call to approve applications
    const response = await accountService.updateAccountApplicationsStatus(requestData);
    console.log('Applications approved successfully:', response);
    
    // Update local data
    const approvedIds = new Set(applicationIds);
    applications.value = applications.value.map(app => {
      if (approvedIds.has(app.applicationId)) {
        return { ...app, status: 'APPROVED' };
      }
      return app;
    });
    
    // Clear selection
    selectedApplications.value = [];
    
    // Show success message
    actionSuccessTitle.value = '批准成功';
    actionSuccessMessage.value = `已成功批准 ${applicationIds.length} 个账户申请！`;
    showSuccessModal.value = true;
    
    // Update counts
    updateCounts();
    
  } catch (error) {
    console.error('Failed to approve applications:', error);
    alert('批准失败，请重试: ' + error.message);
  } finally {
    isApproving.value = false;
  }
}

async function confirmReject() {
  if (!rejectionReason.value.trim()) {
    alert('请输入拒绝原因');
    return;
  }
  
  isRejecting.value = true;
  showRejectModal.value = false;
  
  try {
    // Extract application IDs
    const applicationIds = selectedApplications.value.map(app => app.applicationId);
    console.log('Rejecting applications:', applicationIds);
    
    // Create the request object
    const requestData = {
      applicationIds: applicationIds,
      status: 'REJECTED',
      rejectionReason: rejectionReason.value
    };
    
    // Make API call to reject applications
    const response = await accountService.updateAccountApplicationsStatus(requestData);
    console.log('Applications rejected successfully:', response);
    
    // Update local data
    const rejectedIds = new Set(applicationIds);
    applications.value = applications.value.map(app => {
      if (rejectedIds.has(app.applicationId)) {
        return { ...app, status: 'REJECTED', notes: rejectionReason.value };
      }
      return app;
    });
    
    // Clear selection
    selectedApplications.value = [];
    
    // Show success message
    actionSuccessTitle.value = '拒绝成功';
    actionSuccessMessage.value = `已成功拒绝 ${applicationIds.length} 个账户申请！`;
    showSuccessModal.value = true;
    
    // Update counts
    updateCounts();
    
  } catch (error) {
    console.error('Failed to reject applications:', error);
    alert('拒绝失败，请重试: ' + error.message);
  } finally {
    isRejecting.value = false;
  }
}

function closeSuccessModal() {
  showSuccessModal.value = false;
}

function closeAllModals() {
  showApproveModal.value = false;
  showRejectModal.value = false;
  showSuccessModal.value = false;
}

function navigateBack() {
  console.log('返回管理员首页');
  router.push('/admin');
}

function formatCurrency(value) {
  if (value === undefined || value === null || isNaN(value)) {
    return '¥0.00';
  }
  return '¥' + value.toLocaleString('zh-CN', { minimumFractionDigits: 2, maximumFractionDigits: 2 });
}

function formatDateTime(dateString) {
  if (!dateString) return '-';
  
  try {
    const date = new Date(dateString);
    if (isNaN(date.getTime())) {
      console.warn('Invalid date string:', dateString);
      return '-';
    }
    
    return new Intl.DateTimeFormat('zh-CN', {
      year: 'numeric',
      month: '2-digit',
      day: '2-digit',
      hour: '2-digit',
      minute: '2-digit'
    }).format(date);
  } catch (error) {
    console.error('Error formatting date:', error);
    return '-';
  }
}

// 组件挂载时的初始化
onMounted(() => {
  console.log('账户申请审核页面已加载');
  loadApplications();
});
</script>

<style scoped>
/* Import AG Grid theme */
@import 'ag-grid-community/styles/ag-grid.css';
@import 'ag-grid-community/styles/ag-theme-alpine.css';

.account-approval-page {
  min-height: 100vh;
  background-color: #f8fafc;
  font-family: system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
}

.container {
  max-width: 1400px;
  margin: 0 auto;
  padding: 0 1rem;
}

/* Header */
.page-header {
  background-color: white;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  position: sticky;
  top: 0;
  z-index: 100;
}

.header-content {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 1.5rem 0;
}

.header-left {
  display: flex;
  align-items: center;
  gap: 1rem;
}

.btn-back {
  background-color: transparent;
  border: none;
  color: #4b5563;
  font-size: 1rem;
  cursor: pointer;
  padding: 0.5rem;
  display: flex;
  align-items: center;
  transition: color 0.2s;
}

.btn-back:hover {
  color: #1e40af;
}

.header-info h1 {
  font-size: 1.5rem;
  font-weight: 600;
  color: #1e3a8a;
  margin: 0 0 0.25rem;
}

.header-info p {
  font-size: 0.875rem;
  color: #6b7280;
  margin: 0;
}

.btn-load-applications {
  background-color: #1e40af;
  color: white;
  border: none;
  border-radius: 0.375rem;
  padding: 0.625rem 1.25rem;
  font-size: 0.875rem;
  font-weight: 500;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 0.5rem;
  transition: background-color 0.2s;
}

.btn-load-applications:hover {
  background-color: #1e3a8a;
}

.btn-load-applications:disabled {
  background-color: #93c5fd;
  cursor: not-allowed;
}

/* Main Content */
.page-main {
  padding: 2rem 0;
}

/* Stats Section */
.stats-section {
  display: flex;
  gap: 1.5rem;
  margin-bottom: 2rem;
}

.stat-card {
  background-color: white;
  border-radius: 0.5rem;
  padding: 1.5rem;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
  flex: 1;
  text-align: center;
}

.stat-value {
  font-size: 2rem;
  font-weight: 600;
  color: #1e3a8a;
  margin-bottom: 0.5rem;
}

.stat-label {
  font-size: 0.875rem;
  color: #6b7280;
}

/* Error Message */
.error-message {
  background-color: #fee2e2;
  border-left: 4px solid #ef4444;
  padding: 1rem;
  margin-bottom: 2rem;
  border-radius: 0.25rem;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.error-message p {
  color: #b91c1c;
  margin: 0;
}

.btn-retry {
  background-color: #b91c1c;
  color: white;
  border: none;
  border-radius: 0.25rem;
  padding: 0.5rem 1rem;
  font-size: 0.75rem;
  cursor: pointer;
}

/* Grid Section */
.grid-section {
  background-color: white;
  border-radius: 0.5rem;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
  margin-bottom: 2rem;
  overflow: hidden;
}

.grid-header {
  padding: 1.5rem;
  border-bottom: 1px solid #e5e7eb;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.grid-title {
  display: flex;
  align-items: baseline;
  gap: 0.75rem;
}

.grid-title h2 {
  font-size: 1.25rem;
  font-weight: 600;
  color: #1f2937;
  margin: 0;
}

.grid-count {
  font-size: 0.875rem;
  color: #6b7280;
}

.grid-actions {
  display: flex;
  gap: 1rem;
  align-items: center;
}

.bulk-actions {
  display: flex;
  gap: 0.5rem;
}

.btn-select-all, .btn-clear {
  background-color: #f3f4f6;
  border: 1px solid #d1d5db;
  border-radius: 0.375rem;
  padding: 0.5rem 0.75rem;
  font-size: 0.875rem;
  color: #4b5563;
  cursor: pointer;
  transition: all 0.2s;
}

.btn-select-all:hover, .btn-clear:hover {
  background-color: #e5e7eb;
}

.btn-select-all.active {
  background-color: #dbeafe;
  border-color: #93c5fd;
  color: #1e40af;
}

.btn-clear:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.action-buttons {
  display: flex;
  gap: 0.5rem;
}

.btn-approve, .btn-reject {
  border: none;
  border-radius: 0.375rem;
  padding: 0.625rem 1rem;
  font-size: 0.875rem;
  font-weight: 500;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 0.5rem;
  transition: background-color 0.2s;
}

.btn-approve {
  background-color: #10b981;
  color: white;
}

.btn-approve:hover {
  background-color: #059669;
}

.btn-reject {
  background-color: #ef4444;
  color: white;
}

.btn-reject:hover {
  background-color: #dc2626;
}

.btn-approve:disabled, .btn-reject:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.ag-grid-container {
  height: 500px;
  width: 100%;
}

/* Status badges */
.status-badge {
  display: inline-block;
  padding: 0.25rem 0.5rem;
  border-radius: 9999px;
  font-size: 0.75rem;
  font-weight: 500;
  text-align: center;
}

.status-pending {
  background-color: #fef3c7;
  color: #92400e;
}

.status-approved {
  background-color: #d1fae5;
  color: #065f46;
}

.status-rejected {
  background-color: #fee2e2;
  color: #b91c1c;
}

.status-unknown {
  background-color: #e5e7eb;
  color: #4b5563;
}

/* Empty State */
.empty-state {
  background-color: white;
  border-radius: 0.5rem;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
  padding: 3rem 1.5rem;
  text-align: center;
  margin-bottom: 2rem;
}

.empty-icon {
  font-size: 3rem;
  margin-bottom: 1rem;
}

.empty-state h3 {
  font-size: 1.25rem;
  font-weight: 600;
  color: #1f2937;
  margin: 0 0 0.5rem;
}

.empty-state p {
  font-size: 0.875rem;
  color: #6b7280;
  margin: 0 0 1.5rem;
}

.btn-reload {
  background-color: #1e40af;
  color: white;
  border: none;
  border-radius: 0.375rem;
  padding: 0.625rem 1.25rem;
  font-size: 0.875rem;
  font-weight: 500;
  cursor: pointer;
  transition: background-color 0.2s;
}

.btn-reload:hover {
  background-color: #1e3a8a;
}

/* Loading State */
.loading-state {
  background-color: white;
  border-radius: 0.5rem;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
  padding: 3rem 1.5rem;
  text-align: center;
  margin-bottom: 2rem;
}

.loading-spinner {
  display: inline-block;
  width: 1rem;
  height: 1rem;
  border: 2px solid currentColor;
  border-right-color: transparent;
  border-radius: 50%;
  animation: spin 0.75s linear infinite;
}

.loading-spinner.large {
  width: 3rem;
  height: 3rem;
  border-width: 3px;
  margin-bottom: 1rem;
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

.loading-state p {
  font-size: 0.875rem;
  color: #6b7280;
  margin: 0;
}

/* Modal */
.modal {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  z-index: 1000;
  width: 90%;
  max-width: 500px;
}

.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.5);
  z-index: 999;
}

.modal-content {
  background-color: white;
  border-radius: 0.5rem;
  box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
  overflow: hidden;
}

.modal-header {
  padding: 1.25rem 1.5rem;
  border-bottom: 1px solid #e5e7eb;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.modal-header h2 {
  font-size: 1.25rem;
  font-weight: 600;
  color: #1f2937;
  margin: 0;
}

.modal-close {
  background-color: transparent;
  border: none;
  color: #6b7280;
  font-size: 1.5rem;
  cursor: pointer;
  padding: 0;
  line-height: 1;
}

.modal-body {
  padding: 1.5rem;
}

.selected-applications-preview {
  background-color: #f3f4f6;
  border-radius: 0.375rem;
  padding: 1rem;
  margin: 1rem 0;
}

.preview-header {
  font-size: 0.875rem;
  font-weight: 500;
  color: #4b5563;
  margin-bottom: 0.5rem;
}

.preview-list {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.preview-item {
  font-size: 0.875rem;
  color: #1f2937;
}

.preview-more {
  font-size: 0.75rem;
  color: #6b7280;
  font-style: italic;
  margin-top: 0.25rem;
}

.rejection-reason {
  margin: 1rem 0;
}

.rejection-reason label {
  display: block;
  font-size: 0.875rem;
  font-weight: 500;
  color: #4b5563;
  margin-bottom: 0.5rem;
}

.rejection-reason textarea {
  width: 100%;
  padding: 0.625rem;
  border: 1px solid #d1d5db;
  border-radius: 0.375rem;
  font-size: 0.875rem;
  resize: vertical;
}

.form-actions {
  display: flex;
  justify-content: flex-end;
  gap: 0.75rem;
  margin-top: 1.5rem;
}

.btn-cancel, .btn-confirm {
  border: none;
  border-radius: 0.375rem;
  padding: 0.625rem 1.25rem;
  font-size: 0.875rem;
  font-weight: 500;
  cursor: pointer;
  transition: background-color 0.2s;
}

.btn-cancel {
  background-color: #f3f4f6;
  color: #4b5563;
}

.btn-cancel:hover {
  background-color: #e5e7eb;
}

.btn-confirm {
  background-color: #1e40af;
  color: white;
}

.btn-confirm:hover {
  background-color: #1e3a8a;
}

.btn-confirm.approve {
  background-color: #10b981;
}

.btn-confirm.approve:hover {
  background-color: #059669;
}

.btn-confirm.reject {
  background-color: #ef4444;
}

.btn-confirm.reject:hover {
  background-color: #dc2626;
}

.btn-confirm:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

/* Success Modal */
.success-modal .modal-body {
  text-align: center;
}

.success-icon {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 4rem;
  height: 4rem;
  background-color: #d1fae5;
  color: #059669;
  font-size: 2rem;
  border-radius: 9999px;
  margin: 0 auto 1.5rem;
}

/* Cell Styles */
.application-id-cell {
  font-family: monospace;
  font-weight: 500;
}

.user-id-cell {
  font-family: monospace;
}

.username-cell {
  font-weight: 500;
}

/* Responsive Adjustments */
@media (max-width: 768px) {
  .stats-section {
    flex-direction: column;
    gap: 1rem;
  }
  
  .grid-header {
    flex-direction: column;
    gap: 1rem;
    align-items: flex-start;
  }
  
  .grid-actions {
    width: 100%;
    flex-direction: column;
    gap: 0.75rem;
  }
  
  .bulk-actions, .action-buttons {
    width: 100%;
    justify-content: space-between;
  }
}
</style>
